package com.wn.portal.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.wn.bean.entity.User;
import com.wn.client.UserDubboClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * @author yellow Docter
 * @date 2022 -11-01
 * @desc
 */
@RestController
@Slf4j
@RequestMapping("portal")
public class PortalController {

    @DubboReference(check = false)
    private UserDubboClient userDubboClient;

    @HystrixCommand(fallbackMethod = "errorFallBack")
    @GetMapping("{id}")
    public User findUserById(@PathVariable("id")Integer id) throws Exception {

        if (id == 20){
            throw new Exception("抛出异常");
        }

        return userDubboClient.findUserById(id);
    }

    @PostMapping
    public String addUser(@RequestBody User user){
        return userDubboClient.addUser(user);
    }

    @DeleteMapping("{id}")
    public String deleteById(@PathVariable("id")Integer id){
        return userDubboClient.deleteUser(id);
    }

    /**
     * 服务降级的方法
     * @param id
     * @return
     */
    public User errorFallBack(Integer id){

        User user = new User();
        user.setId(101L);
        user.setUsername("出错了");
        user.setAddress("黄冈是团风林家湾");
        return user;

    }

}
